﻿Imports System
Imports System.CodeDom
Imports System.Globalization
Imports System.Text
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities.Rules
    Friend Class TypeReferenceExpression
        Inherits RuleExpressionInternal
        ' Methods
        Friend Overrides Sub AnalyzeUsage(ByVal expression As CodeExpression, ByVal analysis As RuleAnalysis, ByVal isRead As Boolean, ByVal isWritten As Boolean, ByVal qualifier As RulePathQualifier)
        End Sub

        Friend Overrides Function Clone(ByVal expression As CodeExpression) As CodeExpression
            Dim expression2 As CodeTypeReferenceExpression = DirectCast(expression, CodeTypeReferenceExpression)
            Return New CodeTypeReferenceExpression(TypeReferenceExpression.CloneType(expression2.Type))
        End Function

        Friend Shared Function CloneType(ByVal oldType As CodeTypeReference) As CodeTypeReference
            If (oldType Is Nothing) Then
                Return Nothing
            End If
            Dim result As New CodeTypeReference { _
                .ArrayElementType = TypeReferenceExpression.CloneType(oldType.ArrayElementType), _
                .ArrayRank = oldType.ArrayRank, _
                .BaseType = oldType.BaseType _
            }
            Dim reference2 As CodeTypeReference
            For Each reference2 In oldType.TypeArguments
                result.TypeArguments.Add(TypeReferenceExpression.CloneType(reference2))
            Next
            ConditionHelper.CloneUserData(oldType, result)
            Return result
        End Function

        Friend Overrides Sub Decompile(ByVal expression As CodeExpression, ByVal stringBuilder As StringBuilder, ByVal parentExpression As CodeExpression)
            Dim expression2 As CodeTypeReferenceExpression = DirectCast(expression, CodeTypeReferenceExpression)
            RuleDecompiler.DecompileType(stringBuilder, expression2.Type)
        End Sub

        Friend Overrides Function Evaluate(ByVal expression As CodeExpression, ByVal execution As RuleExecution) As RuleExpressionResult
            Return New RuleLiteralResult(Nothing)
        End Function

        Friend Overrides Function Match(ByVal expression As CodeExpression, ByVal comperand As CodeExpression) As Boolean
            Dim expression2 As CodeTypeReferenceExpression = DirectCast(expression, CodeTypeReferenceExpression)
            Dim expression3 As CodeTypeReferenceExpression = DirectCast(comperand, CodeTypeReferenceExpression)
            Return TypeReferenceExpression.MatchType(expression2.Type, expression3.Type)
        End Function

        Friend Shared Function MatchType(ByVal typeRef1 As CodeTypeReference, ByVal typeRef2 As CodeTypeReference) As Boolean
            If (typeRef1.BaseType <> typeRef2.BaseType) Then
                Return False
            End If
            If (typeRef1.TypeArguments.Count <> typeRef2.TypeArguments.Count) Then
                Return False
            End If
            Dim i As Integer
            For i = 0 To typeRef1.TypeArguments.Count - 1
                Dim reference As CodeTypeReference = typeRef1.TypeArguments.Item(i)
                Dim reference2 As CodeTypeReference = typeRef2.TypeArguments.Item(i)
                If Not TypeReferenceExpression.MatchType(reference, reference2) Then
                    Return False
                End If
            Next i
            Return True
        End Function

        Friend Overrides Function Validate(ByVal expression As CodeExpression, ByVal validation As RuleValidation, ByVal isWritten As Boolean) As RuleExpressionInfo
            Dim expression2 As CodeTypeReferenceExpression = DirectCast(expression, CodeTypeReferenceExpression)
            If (expression2.Type Is Nothing) Then
                Dim item As New ValidationError(Messages.NullTypeType, &H53D)
                item.UserData.Item("ErrorObject") = expression2
                validation.Errors.Add(item)
                Return Nothing
            End If
            If isWritten Then
                Dim error2 As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, New Object() { GetType(CodeTypeReferenceExpression).ToString }), &H17A)
                error2.UserData.Item("ErrorObject") = expression2
                validation.Errors.Add(error2)
                Return Nothing
            End If
            Return New RuleExpressionInfo(validation.ResolveType(expression2.Type))
        End Function

    End Class
End Namespace

